package com.huibei.wateradmin.authority.controller;

import com.huibei.wateradmin.authority.message.ClientRoleInfo;
import com.huibei.wateradmin.authority.message.ClientUserInfo;
import com.huibei.wateradmin.authority.message.ReqDateTablesMsg;
import com.huibei.wateradmin.authority.message.ReqUpdateUserMsg;
import com.huibei.wateradmin.authority.service.RoleService;
import com.huibei.wateradmin.authority.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by wanglinlin on 2016/7/16.
 */
@RestController
@RequestMapping(value = "/admin/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/listPage",method = RequestMethod.GET)
    public ModelAndView listPage() {
        ModelAndView mv = new ModelAndView("user/list");
        Page<ClientRoleInfo> clientRoleInfos = roleService.findByPage(new PageRequest(0, 100));
        mv.addObject("roles", clientRoleInfos);
        return mv;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Page<ClientUserInfo> list(ReqDateTablesMsg msg) {

        Page<ClientUserInfo> userInfoList = userService.findByPage(new PageRequest(msg.getPageNum(),msg.getLength()));
        return userInfoList;

    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public ClientUserInfo detail(@PathVariable Long id) {
        ClientUserInfo userInfo = userService.findClientInfoById(id);
        return userInfo;
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public void updateUser(ReqUpdateUserMsg msg) {

        userService.updateUser(msg.getUserId(),msg.getRoleId(),msg.getCnName());

    }

}
